package geometry
{
	import flash.geom.Point;
	
	import geometry.util.Rect;
	import geometry.util.Vector2d;
	
	public class Polyline implements IGeobject
	{
		// --------------------------------------------------------------------------------------------
		public function Polyline(p: Array)
		{
			_points = p;
		}

		// --------------------------------------------------------------------------------------------
		public function get points():Array{
			return _points;
		}

	  // --------------------------------------------------------------------------------------------
	  /**
	  * Calculate bounding box for given set of points
	  */
		public function get boundingBox():Rect {

			if( _boundingBox == null )
			{
				for each( var point:Point in _points){
					if(_boundingBox == null){
						_boundingBox = new Rect(point.x, point.y);
					}
					else{
						if(point.x < _boundingBox.left)
							_boundingBox.left = point.x;
						else if(point.x > _boundingBox.right)
							_boundingBox.right = point.x;
	
						if(point.y < _boundingBox.top)
							_boundingBox.top = point.y;
						else if(point.y > _boundingBox.bottom)
							_boundingBox.bottom = point.y;
					}
				}
					
				// no points
				if(_boundingBox == null){
					_boundingBox = new Rect(0, 0);
				}
			}
			
			return _boundingBox;	
		}

	  // --------------------------------------------------------------------------------------------
	  /**
	  * Get line length of thi polyline
	  */
		public function get length():Number{
		
			if( _length == -1)
			{
     		var v:Vector2d;
			
		    for(var i:int = 1; i < _points.length; i++) {
	    	  v = Vector2d.points2Vector(_points[i], _points[i-1]);
	    		_length += v.length;
				}
			}
			
			return _length;
		}
		

		// --------------------------------------------------------------------------------------------
		// Private members
		private var _points : Array;
		private var _boundingBox : Rect = null;
		private var _length : Number = -1;
	}
}